﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UL360Admin.Survey.Models;
using UL360DataNET40;

namespace UL360Admin.Survey.Utils
{
    public static class SurveyFormUtil
    {
        public static SaveFormResponse SaveForm(SaveFormRequest request)
        {
            //List of all scale questions
            List<SurveyQuestion> scaleQuestions = new List<SurveyQuestion>();

            //Order count
            int order = 1;

            //Loop through all questions that came with request
            foreach (string number in request.OrderOfScaleSortables)
            {
                //Get question number
                int nNumber;
                int.TryParse(number, out nNumber);

                //Create new scale question
                SurveyQuestion scaleQuestion = new SurveyQuestion { Order = order, QuestionType = (int)QuestionType.Scale };
                
                //Loop through all questionTexts for current scaleQuestion and add them to question
                foreach(QuestionRequest q in request.ScaleQuestionTexts.Where(q => q.QuestionNumber == nNumber).ToList())
                {
                    if (!string.IsNullOrEmpty(q.TextValue))
                    {
                        //Add questiontext to question
                        scaleQuestion.SurveyQuestionTexts.Add(new SurveyQuestionText { Question = q.TextValue, fk_SprakId = q.LanguageId });
                    }
                }

                //Add scale question to scale questions
                scaleQuestions.Add(scaleQuestion);

                //Increment order
                order++;
            }

            //List of all text questions
            List<SurveyQuestion> textQuestions = new List<SurveyQuestion>();

            //Order count
            order = 1;

            //Loop through all questions that came with request
            foreach (string number in request.OrderOfTextSortables)
            {
                //Get question number
                int nNumber;
                int.TryParse(number, out nNumber);

                //Create new scale question
                SurveyQuestion textQuestion = new SurveyQuestion { Order = order, QuestionType = (int)QuestionType.Text };

                //Loop through all questionTexts for current scaleQuestion and add them to question
                foreach (QuestionRequest q in request.TextQuestionTexts.Where(q => q.QuestionNumber == nNumber).ToList())
                {
                    if (!string.IsNullOrEmpty(q.TextValue))
                    {
                        //Add questiontext to question
                        textQuestion.SurveyQuestionTexts.Add(new SurveyQuestionText { Question = q.TextValue, fk_SprakId = q.LanguageId });
                    }
                }

                //Add scale question to scale questions
                textQuestions.Add(textQuestion);

                //Increment order
                order++;
            }

            using (var db = new UL360Entities())
            {
                db.SurveyForms.Add(new SurveyForm
                {
                    DateCreated = DateTime.Now,
                    SurveyType = (int)request.FormType,
                    SurveyQuestions = scaleQuestions.Concat(textQuestions).ToList()
                });

                db.SaveChanges();
            }

            return new SaveFormResponse { Status = ResponseStatus.SUCCESS };
        }
    }
}